<script setup>
import ActivityNoteList from "@/views/contract/common/activityNote/activityNoteList.vue";
import {
  changeFileTemplate,
  deleteCon,
  draftComplete,
  getConMyTemplatePageList
} from "@/api/contract/myContract.js";

import UploadFile from "@/views/common/uploadFile.vue";
import {pageofficeOpenFile} from "@/plugins/pageoffice/pageoffice-common.js";
import {backMyConList} from "@/utils/backList.js";
import {filemakerctrl} from "js-pageoffice";
import SelectTemplateList from "@/views/common/selectTemplateList.vue";
import {selectTemplateFunctions} from "@/utils/selectTemplate.js";
import {setCurrentIsRefreshActivityNote} from "@/utils/contract-my.js";

const {proxy} = getCurrentInstance()
const loadMainViewData = inject('loadMainViewData')
const loading = ref(false)
const templateListOpen = ref(false)
const prop = defineProps({
  conId: {
    type: Number
  },
  flowInfoButtons: {
    type: JSON
  },
  isRefreshActivityNote: {
    type: Boolean,
  }
})
const currentIsRefreshActivityNote = ref(prop.isRefreshActivityNote)
watchEffect(() => {
  if (prop.isRefreshActivityNote) {
    setCurrentIsRefreshActivityNote(currentIsRefreshActivityNote)
  }
})
const conId = prop.conId
const {
  openTemplateDialog,
  closeTemplateDialog,
  getTemplateList
} = selectTemplateFunctions(getConMyTemplatePageList, '选择合同模板', templateListOpen)

//上传文件所需参数
const uploadFileRef = ref(null)
const uploadParams = ref({
  url: import.meta.env.VITE_APP_BASE_API + '/con/my/changeConFile',
  title: "更换合同正文",
  accept: '.doc, .docx, .pdf',
  name: 'conFile',
  data: {id: conId}
})
function updateFile() {
  uploadFileRef.value.openUploadDialog()
}

function uploadFileSuccess() {
  setCurrentIsRefreshActivityNote(currentIsRefreshActivityNote)
}

function openFile() {
  pageofficeOpenFile('/contract/my/draft/openFile/' + conId, conId)
}

function handleNext() {
  proxy.$modal.confirmAndExecuteJS('确认已起草完毕，进入审核中？', draftComplete, conId, ()=> {
    loadMainViewData()
  })
}

function handleSelectedTemplate(template) {
  templateListOpen.value = false
  loading.value = true
  const query = {id: conId, templateId: template.id}
  changeFileTemplate(query).then(() => {
    wordAutoFill(conId)
  }).catch((error) => {
    loading.value = false
  })
}

function wordAutoFill(conId) {
  filemakerctrl.SaveFilePage = '/con/my/updateDraftFile?id=' + conId
  filemakerctrl.CallFileMaker({
    url: "/con/my/wordAutoFill/" + conId,
    success: function () {
      loading.value = false
      setCurrentIsRefreshActivityNote(currentIsRefreshActivityNote)
      proxy.$modal.msgSuccess("模板更换成功")
    },
    error: function (msg) {
      loading.value = false
      proxy.$modal.alertError(msg)
    }
  })
}

function handleDelete() {
  proxy.$modal.confirmAndExecuteJS('确认要删除当前合同记录？', deleteCon, conId, backMyConList)
}

</script>

<template>
  <el-row v-loading="loading" element-loading-text="正在执行，请稍后...">
    <el-col :span="12" class="left">
      <el-row>
        <el-form-item>
          <el-button @click="backMyConList">返 回</el-button>
          <el-button type="primary" @click="openFile">打开正文</el-button>
          <el-button type="success" @click="handleNext" v-if="flowInfoButtons.isNext">起草完毕</el-button>
        </el-form-item>
      </el-row>
    </el-col>
    <el-col :span="12">
      <el-row justify="end">
        <el-form-item>
          <el-button type="primary" @click="openTemplateDialog" v-if="flowInfoButtons.isChangeFileTemplate">更换模板</el-button>
          <el-button type="primary" @click="updateFile" v-if="flowInfoButtons.isChangeFile">更换正文</el-button>
          <el-button type="info" @click="handleDelete" v-if="flowInfoButtons.isDelete">删 除</el-button>
        </el-form-item>
      </el-row>
    </el-col>
  </el-row>

  <UploadFile ref="uploadFileRef" :uploadParams="uploadParams" @uploadFileSuccess="uploadFileSuccess"/>

  <SelectTemplateList
      :is-open="templateListOpen"
      @getTemplateList="getTemplateList"
      @handleSelectedTemplate="handleSelectedTemplate"
      @close="closeTemplateDialog"
      templateType="contract"/>

  <!-- 流程信息列表 -->
  <ActivityNoteList :isRefreshActivityNote="currentIsRefreshActivityNote" :isCanAddActivityNote="true"/>
</template>

